package com.westos.echufang.service.updatePwd;

import com.mysql.cj.util.StringUtils;
import com.westos.echufang.constants.RoleConstants;
import com.westos.echufang.dao.AdminDao;
import com.westos.echufang.dao.EmployeeDao;
import com.westos.echufang.pojo.entity.Admin;
import com.westos.echufang.pojo.entity.Employee;
import com.westos.echufang.utils.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
public class PwdServiceImpl implements PwdService {
    @Autowired
    AdminDao adminDao;
    @Autowired
    EmployeeDao employeeDao;

    private final BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    public PwdServiceImpl(BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }
    /**
     *
     * @param newPwd 新密码
     * @return a=0 修改成功  1 修改失败   2  代表newPwd为空
     */
    @Override
    public Integer updatePwd(String newPwd) {
        int a=0;
        int i=0;
        if(!StringUtils.isNullOrEmpty(newPwd)){
            Admin admin = RoleConstants.admin;
            String job="";
            System.out.println(newPwd);
            newPwd= bCryptPasswordEncoder.encode(newPwd); //进行加密
           // System.out.println("newPwd:"+newPwd);
            Employee employee = RoleConstants.employee;

            if (admin!=null){
                job=admin.getJob();
            }
            if (employee!=null){
                job=employee.getJob();
            }

            if (StringUtils.isNullOrEmpty(job)){
                //i=1;
               i= adminDao.updatePwdById(admin.getId(), newPwd);

            }else if (job.equals("pharmacist")||job.equals("doctor")){
               // i=2;
              i=employeeDao.updatePwdById(newPwd,employee.getId());
            }


            if (i>0){
               // System.out.println("i:"+i);  //管理员输出1
                a=0;
            }else {
               // System.out.println("i:"+i); //其他输出2
                a=1;
            }
        }else{
            a=2;  //密码为空
        }
        return a;
    }

    /**
     *
     * @param oldPwd 旧密码
     * @param newPwd  新密码
     * @return  a=1 及oldPwd与数据库password数据相同  2 及oldPwd与数据库password数据不同  3 新旧密码相同 4 新旧密码不同
     */
    @Override
    public Integer checkPwd(String oldPwd, String newPwd) {
        int a=0;
        Admin admin = RoleConstants.admin;
        String password=null;
        Employee employee = RoleConstants.employee;
        if (admin!=null){
            password=admin.getPassword();
        }
        if (employee!=null){
            password=employee.getPassword();
        }
        if(oldPwd!=null){

            //测试时：if (oldPwd.equals("123456"))
            if( bCryptPasswordEncoder.matches(oldPwd, password)){
                a=1; //认证成功
            }else{
                a=2; //认证失败
            }
        }
        if (newPwd!=null){
            //测试时：if (newPwd.equals("123456"))
            if ( bCryptPasswordEncoder.matches(newPwd,password)){
                a=3; //若新密码与旧密码相同时，返回a为3
            }else{
                a=4; //不同时，返回a为4
            }
        }
        return a;
    }


}
